package com.huawei.codearts;


import com.huawei.codearts.tools.utils.SettingUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.sql.*;

public class MySQLDemo {
    // JDBC 驱动名及数据库 URL
//    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String JDBC_DRIVER = SettingUtils.getProperty("spring.datasource.driver-class-name");
    static final String DB_URL = SettingUtils.getProperty("spring.datasource.url");;
    // 数据库的用户名与密码
    static final String USER = SettingUtils.getProperty("spring.datasource.username");
    static final String PASS = SettingUtils.getProperty("spring.datasource.password");
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            // 执行查询
            System.out.println(" 实例化Statement对...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, user, name, status, money FROM qhdzs_member where id=53";
            ResultSet rs = stmt.executeQuery(sql);
            // 展开结果集数据库
            while(rs.next()) {
                // 通过字段检索
                int id  = rs.getInt("id");
                String user = rs.getString("user");
                String name = rs.getString("name");
                int status = rs.getInt("status");
                BigDecimal money = rs.getBigDecimal("money");
                // 输出数据
                String statusStr = status == 1 ? "正常" : (status == 2 ? "未验证" : "禁用");
                System.out.print("ID: " + id);
                System.out.print(", 账号: " + user);
                System.out.print(", 姓名: " + name);
                System.out.print(", 余额: " + money);
                System.out.print(", 状态: " + statusStr);
                System.out.print("\n");
                // JSONObject输出json
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", id);
                jsonObject.put("user", user);
                jsonObject.put("name", name);
                jsonObject.put("money", money);
                jsonObject.put("status", statusStr);
                String json = jsonObject.toString();
                System.out.println(json);
                // JSONArray输出json
                JSONArray jsonArray1 = new JSONArray();
                jsonArray1.put(id);
                jsonArray1.put(user);
                jsonArray1.put(name);
                jsonArray1.put(money);
                jsonArray1.put(statusStr);
                String json2 = jsonArray1.toString();
                System.out.println(json2);
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        } catch(SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if(stmt!=null)
                    stmt.close();
            } catch(SQLException se2) {

            }
            // 什么都不做
            try {
                if(conn!=null)
                    conn.close();
            } catch(SQLException se) {
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}
